<!--  -->
<template>
  <div class="clazz">
    <el-card shadow="hover" class="card1" :body-style="{ padding: '20px' }">
      <div slot="header">
        <span>搜索条件</span>
      </div>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input v-model="classSearch.className" size="small" placeholder="请输入班级名搜索"></el-input>
        </el-col>
        <el-col :span="6" :offset="1">
          <el-select style="width:100%" size="small" v-model="classSearch.type">
            <el-option label="前端" value="0"></el-option>
            <el-option label="UI" value="1"></el-option>
          </el-select>
        </el-col>
      </el-row>
      <el-row class="secondRow" :gutter="20">
        <el-col :span="6" :offset="20">
          <el-button size="small" @click="removeSearch">重置</el-button>
          <el-button size="small" type="primary" @click="toSearch">搜索</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-card
      class="box-card card2"
      v-loading="loading"
      :body-style="{ padding: '20px'}"
      shadow="never"
    >
      <div slot="header" class="clearfix">
        <span>班级信息</span>
        <el-button style="float:right" type="primary" @click="addClass" size="small">新增</el-button>
      </div>
      <div>
        <el-table :data="classLists" border style="width: 100%">
          <el-table-column type="index" width="80"></el-table-column>
          <el-table-column prop="className" label="班级名称" width="120"></el-table-column>
          <el-table-column label="班级类型" width="120">
          <template slot-scope="scope">
            <div>
              <p v-if="scope.row.type==0" style="color:blue">前端</p>
              <p v-if="scope.row.type==1" style="color:red">UI</p>
            </div>
          </template>
          </el-table-column>
          <el-table-column prop="desc" label="班级描述" width="200"></el-table-column>
          <el-table-column prop="startTime" label="开班时间" width="200"></el-table-column>
          <el-table-column prop="endTime" label="结束时间" width="200"></el-table-column>
          <el-table-column prop="addTime" label="添加时间" width="200"></el-table-column>
          <el-table-column fixed="right" label="操作" width="220">
            <template slot-scope="scope">
              <el-button size="small">查看</el-button>

              <el-button type="primary" size="small" @click="handleClick(scope.row.id)">编辑</el-button>
              <el-button type="danger" size="small" @click="removeClass(scope.row.id)">删 除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="margin:20px 300px"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.start"
          :page-sizes="page.sizes"
          :page-size="page.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.pages"
          class="step"
        ></el-pagination>
      </div>
    </el-card>
    <Details ref="classDetails" @toInit="init"></Details>
  </div>
</template>

<script>
import Details from "@/views/student/clazz/ClassDetails";
import { classList, removeClassList, numZhuan } from "@/api/students/classList";
const _ = require("lodash");

export default {
  name: "Clazz",
  components: {
    Details,
  },
  data() {
    return {
      classSearch: {},
      search2: {},
      page: {
        start: 1,
        limit: 10,
        sizes: [10, 20, 30],
      },
      classLists: [],
    };
  },
  created() {
    this.init();
  },
  methods: {
    removeClass(id) {
      var that = this;
      this.$confirm("确定要删除吗", "确认信息", {
        distinguishCancelAndClose: true,
        confirmButtonText: "删除",
        cancelButtonText: "算了兄弟",
      })
        .then(() => {
          removeClassList({ id: id }).then((res) => {
            console.log(res);
            if (res.code == true) {
              this.$message({
                type: "success ",
                message: `删除成功`,
              });
              that.init();
            }
          });
        })
        .catch((action) => {});
    },
    handleClick(id) {
      this.$refs.classDetails.openDetauls(id);
    },
    handleSizeChange(e) {
      this.page.limit = e;
      this.init();
    },
    handleCurrentChange(e) {
      this.page.start = e;
      this.init();
    },
    addClass() {
      this.$refs.classDetails.openAdd();
    },
    removeSearch() {
      this.classSearch = {};
      this.search2 = {};
      this.init();
    },
    init() {
      var obj = {
        start: this.page.start,
        limit: this.page.limit,
        ...this.search2,
      };
      this.loading = true;
      classList(obj).then((res) => {
        this.classLists = res.data.list;
        this.page.pages = res.data.totalRow;
        this.loading = false;
      });
    },
    toSearch() {
      this.search2 = _.pickBy(this.classSearch, (item) => item);
      this.page.start = 1;
      this.init();
    },
  },
};
</script>

<style  lang='scss' scoped >
.clazz {
  padding: 20px;
  .card1 {
    border: 0;
    margin-top: 20px;
    .secondRow {
      margin-top: 20px;
    }
  }
  .card2 {
    border: 0;
    margin-top: 50px;
  }
}
</style>
